<?php
namespace app\admin\controller;


use think\Controller;
use think\Db;
use think\Request;
use think\Session;
class Role extends Controller  //控制器
{
    /**
     * 角色列表
     */
    public function index()
    {
    	if(!in_array(strrchr(__CLASS__."/".__FUNCTION__,"\\"),Session::get('admin_pri','admin'))){
        	 $this->error('你没有权限访问角色列表', url('/main'), []);
        }
        $list = Db::name('rbac_role')
            ->field(['id', 'role_name','role_coment'])
            ->select();
            
        $pri=Db::name('rbac_privilege')
            ->field(['id','pri_name'])
            ->select();

        //模板
        return view('role/index', [
            'title' => '角色列表',
            'add' => '添加角色',
            'list' => $list,
            'pri'=>$pri
        ]);
    }

    /**
     * 添加角色页面
     */
    public function create()
    {
    	if(!in_array(strrchr(__CLASS__."/".__FUNCTION__,"\\"),Session::get('admin_pri','admin'))){
        	 $this->error('你没有权限添加角色', url('/main'), []);
        }
         $list = Db::name('rbac_privilege')
            ->field(['id','pri_name'])
            ->select();

        //模板
        return view('role/create', [
            'list' => $list
        ]);
    }


    /**
     * 执行添加
     */
    public function docreate(Request $request)
    {
        $p = $request->post();
        unset($p['pri']);
        $pri=Request::instance()->post('pri/a'); 
        
         Db::startTrans();
         try{
         	Db::name('rbac_role')
            ->insert($p);
            
            $roleId = Db::name('rbac_role')->getLastInsID();
		     if(!empty($pri)){
		        	  foreach($pri as $v){
		        			$ar=array('pri_id'=>$v,'role_id'=>$roleId);
		        			  $rows = Db::name('rbac_role_privilege')
		           			 	->insert($ar);
		        		}
		        }
		    Db::commit(); 
      		return true;
         }catch(Exception $e){
         	Db::rollback();
         	return false;
         } 
         
       }
    /**
     * 执行删除
     */
    public function del(Request $request){
    	if(!in_array(strrchr(__CLASS__."/".__FUNCTION__,"\\"),Session::get('admin_pri','admin'))){
        	return "no pri";
        }
        
    	$id=$request->get()['id'];
    	
    	Db::startTrans();
    	try{
    		$rows = Db::name('rbac_role')
    	 	->where('id','=',$id)
            ->delete();
	        $res = Db::name('rbac_admin_role')->where('role_id',$id)->delete();
	        $res = Db::name('rbac_role_privilege')->where('role_id',$id)->delete();
    		Db::commit();
    		return true;
    	}catch(Exception $e){
    		Db::rollback();
    		
    		return false;
    	}
    	
    }
    
    /*
     * 获取详情
     */
    
    public function edit(Request $request){
    	if(!in_array(strrchr(__CLASS__."/".__FUNCTION__,"\\"),Session::get('admin_pri','admin'))){
        	return "no pri";
        }
    	$id=$request->get()['id'];
    	$res=Db::name('rbac_role')
    		->where('id','=',$id)
            ->select();
        $data=$res[0];
        
        $pri=Db::name('rbac_role_privilege')
    		->where('role_id','=',$id)
            ->select();
            
        if(!empty($pri)){
        	foreach($pri as $k=>$v){
        		$data['pri'][]=$v['pri_id'];
        	}
        }
            
     
        
        if(is_null($data)){
        	return false;
        }else{
        	 return json($data);
        }
    	
    	
    }
    
    /*
     * 编辑
     */
    
    public function doEdit(Request $request){
    	if(!in_array(strrchr(__CLASS__."/".__FUNCTION__,"\\"),Session::get('admin_pri','admin'))){
        	return "no pri";
        }
    	//插入角色表数据
    	$p = $request->post();
    	unset($p['pri']);
    	$pri=$request->post()['pri'];
    	$id=$p['id'];
    	
    	Db::startTrans();
    	try{
    		Db::name('rbac_role')->update($p);
    		Db::name('rbac_role_privilege')->where('role_id',$id)->delete();
    		if(!empty($pri)){
	        	  foreach($pri as $v){
	        			$ar=array('pri_id'=>$v,'role_id'=>$id);
	        			  $rows = Db::name('rbac_role_privilege')
	           			 	->insert($ar);
	           			
	        		}
	        }
	        Db::commit();
	        
	        return true;
    	}catch (Exception $e){
    		Db::rollback();
    		
    		return "失败";
    	}
    	
    	
		
    	
    	
    }

}
?>